/**
 * UEvent 是一个非常轻量的事件驱动库
 */
#ifndef UEVENT_H_20180118
#define UEVENT_H_20180118

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

struct UEventLoop;
typedef struct UEventLoop UEventLoop;

//! 返回码
enum UEventRetCode {
    kUEventOK = 0,
    kUEventFileDescNotExist,
    kUEventTimerNotExist,
    kUEventTimerExist,
    kUEventInvalidArgument,
    kUEventOutOfMemery,
    kUEventDeleteRunningLoop,
    kUEventCreatePipeFail,
    kUEventSelectUnexpectBreak,
    kUEventExecuteTimeExpired,
};
typedef enum UEventRetCode UEventRetCode;

//! 文件描述符相关
enum UEventOperation {
    kUEventOperationNone    = 0x00,
    kUEventOperationRead    = 0x01,
    kUEventOperationWrite   = 0x02,
    kUEventOperationExcept  = 0x04,
};
typedef enum UEventOperation eUEventOperation;

typedef void (*UEventFileDescCallback) (int fd, eUEventOperation operation, void *arg);

struct UEventFileDescDetail {
    uint8_t operation_bits;
    UEventFileDescCallback callback_func; 
    void *callback_arg; 
};
typedef struct UEventFileDescDetail UEventFileDescDetail;

//! 定时器相关
typedef void (*UEventTimerCallback) (int timer_id, void *arg);

struct UEventTimerDetail {
    UEventTimerCallback callback_func; 
    void *callback_arg; 
};
typedef struct UEventTimerDetail UEventTimerDetail;

//! 函数列表
UEventLoop*     UEvent_NewLoop();
UEventRetCode   UEvent_RunLoop(UEventLoop *loop);
UEventRetCode   UEvent_BreakLoop(UEventLoop *loop);
UEventRetCode   UEvent_DeleteLoop(UEventLoop *loop);

UEventRetCode   UEvent_ReadFileDescDetail(UEventLoop *loop, int fd, UEventFileDescDetail *detail);
UEventRetCode   UEvent_UpdateFileDesc(UEventLoop *loop, int fd, const UEventFileDescDetail *detail);
UEventRetCode   UEvent_RemoveFileDesc(UEventLoop *loop, int fd);

UEventRetCode   UEvent_RunAt   (UEventLoop *loop, int timer_id, uint32_t execute_time_ms, const UEventTimerDetail *detail);
UEventRetCode   UEvent_RunAfter(UEventLoop *loop, int timer_id, uint16_t wait_time_ms, const UEventTimerDetail *detail);
UEventRetCode   UEvent_RunEvery(UEventLoop *loop, int timer_id, uint32_t interval_ms, const UEventTimerDetail *detail);
UEventRetCode   UEvent_RunTimer(UEventLoop *loop, int timer_id, uint32_t wait_time_ms, uint32_t interval_ms, const UEventTimerDetail *detail);
UEventRetCode   UEvent_StopTimer(UEventLoop *loop, int timer_id);

#ifdef __cplusplus
}
#endif

#endif //UEVENT_H_20180118
